script_enemy_main{

let shot1=0;
let bullet1=[];
let timer1=[];
let distance1=[];

let shoottime=20;
let shotse=0;

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=7;
let spellcardnumber=61;
let damagerate=10;
let outfit=1;
outfit=(128*outfit)-128;
let usespell=0;
let bgfade=0;
let frame=0;
let time=0;
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;

let SEshots5=("\script\SoundEffects\shots5.wav");
let SEshotb1=("\script\SoundEffects\shotb1.wav");
let SEshotb7=("\script\SoundEffects\shotb7.wav");

let BG1=("\script\Images\BackgroundLayers\Nightmare2.png");
let BG2=("\script\Images\BackgroundLayers\Nightmare3.png");
let GRboss=("\script\Images\CharacterSprites\"~character~".png");

#include_function "script/Functions/SetSpellcardCommonData.txt";
#include_function "script/Functions/SpellcardNameLoad.txt";
#include_function "script/Functions/HealthBarLoad.txt";
#include_function "script/Functions/CutInLoad.txt";

@Initialize{
	LoadUserShotData("script\shots\ShotsNightmare1.txt");

	LoadSE("\script\SoundEffects\shots5.wav");
	LoadSE("\script\SoundEffects\shotb1.wav");
	LoadSE("\script\SoundEffects\shotb7.wav");
	
	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare2.png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare3.png");

	SetScore(1000000);
	SetLife(400);
	SetTimer(75);
	SetInvincibility(120);
	SetDamageRate(10,10);
	SetEnemyMarker(true);
	MagicCircle(true);
	#include_function "script/Functions/Focus.txt";
	Focus(character);
	SetEffectForZeroLife(60,100,1);
	SetMovePosition02(cx,miny+100,50);
}
	
@MainLoop{

SetCollisionA(GetX,GetY,16);
SetCollisionB(GetX,GetY,16);
SetShotAutoDeleteClip(64,64,64,64);

if(GetCommonData("BombOn")==0){ damagerate=10; if(GetTimer<50){damagerate=25; } }
if(GetCommonData("BombOn")==1){ damagerate=0; }
SetDamageRate(damagerate,damagerate);

let difficulty="";
if(GetCommonDataDefault("Difficulty",2)==1){ difficulty="Easy"; }
if(GetCommonDataDefault("Difficulty",2)==2){ difficulty="Normal"; }
if(GetCommonDataDefault("Difficulty",2)==3){ difficulty="Hard"; }
if(GetCommonDataDefault("Difficulty",2)==4){ difficulty="Lunatic"; }

SpellcardName("Sleep Paralysis [Kanashibari]",spellcardnumber); 
HealthBar();
Portrait(cutin,1);

if(time%60==0 && time>=60){
	if(GetPlayerX>minx+50 && GetPlayerX<maxx-50){
	SetMovePosition01(GetPlayerX+rand(-30,30),rand(miny+80,miny+120),1.5);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(15,30),rand(miny+80,miny+120),1.5);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(15,30),rand(miny+80,miny+120),1.5);
	}
}


if(time%180==0 && time>=60){
let angle=GetAngleToPlayer;
	loop(17){
	CreateShot01(GetX,GetY,1.5,angle,111,10);
	let angle2=rand(0,360);
		loop(10){
		CreateShot01(GetX+23*cos(angle2),GetY+23*sin(angle2),1.5,angle,87,10);
		angle2+=360/10;
		}
	angle+=360/17;
	}
usespell-=20;
PlaySE(SEshotb1);
}

if((time+90)%180==0 && time>=60){
let angle=GetAngleToPlayer+(360/40);
	loop(20){
	CreateShot01(GetX,GetY,1.5,angle,97,10);
	let angle2=rand(0,360);
		loop(14){
		CreateShot01(GetX+17*cos(angle2),GetY+17*sin(angle2),1.5,angle,73,10);
		angle2-=360/14;
		}
	angle+=360/20;
	}
usespell-=20;
PlaySE(SEshotb7);
}


if(frame>=shoottime && time>=90){
let radius=rand(80,200);
let angle=rand(0,360);
SetShotColor(170,170,170);
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetPlayerX+radius*cos(angle),GetPlayerY+radius*sin(angle));
	Obj_SetAngle(shot1,angle+180);
	Obj_SetSpeed(shot1,rand(2,5));
	ObjShot_SetDelay(shot1,20);
	ObjShot_SetBombResist(shot1,true);
	ObjShot_SetGraphic(shot1,71);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;
	distance1=distance1~[shot1];
	distance1[length(bullet1)-1]=rand_int(30,50);
SetShotColor(255,255,255);
if(shoottime>5){ shoottime--; }
frame=0;
if(shotse==0){ PlaySE(SEshots5); shotse=6; }
}
if(shotse>0){ shotse--; }

let i=0;
while(i<length(bullet1)){
	if(Obj_BeDeleted(bullet1[i])){
	bullet1=erase(bullet1,i); timer1=erase(timer1,i); distance1=erase(distance1,i);
	i--;
	}
	else{
		if((((GetPlayerX-Obj_GetX(bullet1[i]))^2+(GetPlayerY-Obj_GetY(bullet1[i]))^2)^0.5)<distance1[i]){ Obj_SetSpeed(bullet1[i],0); }
		if(timer1[i]>=700){ ObjShot_FadeDelete(bullet1[i]); }
	timer1[i]=timer1[i]+1;
	}
i++;
}


time++; frame++;
if(usespell>0){ usespell--; } if(usespell<0){ usespell++; }
SetCommonData("Boss1X",GetX); SetCommonData("Boss1Y",GetY);

#include_function "script/Functions/SpellcardName.txt";
#include_function "script/Functions/HealthBar.txt";
#include_function "script/Functions/CutIn.txt";
}

@BackGround{
	if(bgfade<255){ bgfade+=5; }

	SetGraphicScale(2,2);
	SetRenderState(ALPHA);
	SetAlpha(bgfade);
	SetColor(255,255,255);
	SetTexture(BG1);
	SetGraphicRect(0,0,300,300);
	SetGraphicAngle(0,0,-time);
	DrawGraphic(cx,cy);

	SetGraphicScale(2,2);
	SetRenderState(ALPHA);
	SetAlpha(bgfade/2);
	SetColor(255,255,255);
	SetTexture(BG2);
	SetGraphicRect(0,0,300,300);
	SetGraphicAngle(0,0,time*1.1);
	DrawGraphic(cx,cy);
}

@DrawLoop{
	SetGraphicScale(1,1);
	SetTexture(GRboss);
	SetGraphicAngle(0,0,0);
	SetColor(120,120,120);
	SetRenderState(ALPHA);

	if(usespell>=1){ SetGraphicRect(384,outfit,512,outfit+128); }
	if(usespell<=-1){ SetGraphicRect(512,outfit,640,outfit+128); }
	if(usespell==0){
		if(GetSpeedX<=0.5 && GetSpeedX>=-0.5){ SetGraphicRect(0,outfit,128,outfit+128); }
		else if(GetSpeedX<-0.5){ SetGraphicRect(128,outfit,256,outfit+128); }
		else if(GetSpeedX>0.5){ SetGraphicRect(256,outfit,384,outfit+128); }
	}
	DrawGraphic(GetX,GetY);
}

@Finalize{
	SetCommonData("NightmareSpellcards",5);
	NewPointData(spellcardnumber,6);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}